/**  
* @Title: WechatSiteUserOrderServiceImpl.java
* @Package com.hjb.service.impl.Wechat
* @author <a href="mailto:125671254@qq.com">Pan Xinwen</a>panxinwen
* @date 2018年1月13日 下午3:01:20
* @version V1.0  
*/ 
package com.hjb.service.impl.Wechat;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hjb.service.Wechat.WechatSiteUserOrderService;
import com.hjb.service.app.BaseAreaService;
import com.hjb.service.domain.InstalmentOrder.InstalmentOrder;
import com.hjb.service.domain.InstalmentOrder.InstalmentOrderExample;
import com.hjb.service.domain.SiteUserOrder.SiteUserOrder;
import com.hjb.service.domain.SiteUserOrder.SiteUserOrderExample;
import com.hjb.service.domain.siteuser.SiteUser;
import com.hjb.service.mapper.InstalmentOrderMapper;
import com.hjb.service.mapper.SiteUserMapper;
import com.hjb.service.mapper.SiteUserOrderMapper;
import com.hjb.service.utils.StringUtil;

/**
* @ClassName: WechatSiteUserOrderServiceImpl
* @Description: TODO(租户合同维护)
* @author <a href="mailto:125671254@qq.com">Pan Xinwen</a>panxinwen
* @date 2018年1月13日 下午3:01:20
*
*/
@Service
public class WechatSiteUserOrderServiceImpl implements WechatSiteUserOrderService {
	
	@Autowired
	private SiteUserOrderMapper siteUserOrderMapper;
	@Autowired
	private BaseAreaService baseAreaService;
	@Autowired
	private SiteUserMapper siteUserMapper;
	@Autowired
	private InstalmentOrderMapper instalmentOrderMapper;
	@Override
	public SiteUserOrder getUserOrder(String tenancyMobile,Integer status) {
		SiteUserOrderExample example = new SiteUserOrderExample();
		example.createCriteria().andTenancyMobileEqualTo(tenancyMobile).andStatusNotEqualTo(6).andStatusNotEqualTo(8);
//		SiteUserOrder record = new SiteUserOrder();
//		record.setTenancyMobile(tenancyMobile);
//		record.setStatus(2);
		List<SiteUserOrder> list = siteUserOrderMapper.selectByExample(example);
		if(list==null || list.size()==0){
			return null;
		}else{
			
			SiteUserOrder siteUserOrder = list.get(0);
			
			return formatOrder(siteUserOrder);
		}
	}
	
	
	private SiteUserOrder formatOrder(SiteUserOrder siteUserOrder){
		
		Map<String,Object> area = baseAreaService.getBaseAreasByRegionNo(siteUserOrder.getCityNo());
		
		String province = (String) ((Map)area.get("province")).get("name");
		String city = (String) ((Map)area.get("city")).get("name");
		String region = (String) ((Map)area.get("region")).get("name");
		
		if(!StringUtil.isEmpty(province) && !StringUtil.isEmpty(city)&& !StringUtil.isEmpty(region)){
			if(province.equals(city)){
				siteUserOrder.setHouseName(city+"市"+region+"区"+siteUserOrder.getHouseName());
			}else{
				siteUserOrder.setHouseName(province+city+"市"+region+"区"+siteUserOrder.getHouseName());
			}
		}
		
		siteUserOrder.setCityNo(siteUserOrder.getCityNo());
		
		String[] timeOffset = siteUserOrder.getTimeOffset().split(",");
		
		siteUserOrder.setTimeOffset((timeOffset[0].equals("0")?"":timeOffset[0]+"个月")+(timeOffset[1].equals("0")?"":timeOffset[1]+"天"));
		
		return siteUserOrder;
		
	}

	@Override
	public Integer verifySiteUserOrder(String id) {
		SiteUserOrder siteUserOrder  = siteUserOrderMapper.selectByPrimaryKey(id);
		siteUserOrder.setStatus(3);
		return siteUserOrderMapper.updateByPrimaryKeySelective(siteUserOrder);
	}


	@Override
	public Map<String, Object> getElectronicContract(String userId, String orderId) {
		
		//获取租户信息
		SiteUser siteUser = siteUserMapper.selectByPrimaryKey(Integer.parseInt(userId));
		
		SiteUserOrder siteUserOrder = siteUserOrderMapper.selectByPrimaryKey(orderId);
		
		InstalmentOrderExample example = new InstalmentOrderExample();
		example.createCriteria().andOrderIdEqualTo(orderId);
		example.setOrderByClause("term_no");
		List<InstalmentOrder> list = instalmentOrderMapper.selectByExample(example);
		
		Date orderCreateTme = siteUserOrder.getGmtCreate();
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put("name", siteUser.getName());//租戶姓名
		map.put("cardNumber", siteUser.getCardNumber());//租戶身份證號
		
		Calendar now =  Calendar.getInstance();
		now.setTime(orderCreateTme);
		
		map.put("signYear", now.get(Calendar.YEAR));//合同年
		map.put("signMonth", now.get(Calendar.MONTH)+1);//合同月
		map.put("signDay", now.get(Calendar.DAY_OF_MONTH));//合同日
		
		map.put("term", siteUserOrder.getPayTerm());//期數
		map.put("aCash", siteUserOrder.getCash().intValue()*siteUserOrder.getPayTerm());//總租金
		map.put("mCash", siteUserOrder.getCash().intValue());//月租金
		
		map.put("interest", 0);//月服务费
		map.put("amount", siteUserOrder.getCash().intValue());//月总金额
		
		list.stream().findFirst().ifPresent(ins ->{
		map.put("interest", ins.getInterestAmt());
		map.put("amount", siteUserOrder.getCash().intValue()+ins.getInterestAmt());
		Calendar calendar =  Calendar.getInstance();
		now.setTime(ins.getRepayDate());
		map.put("fYear", calendar.get(Calendar.YEAR));//首个还款年
		map.put("fMonth", calendar.get(Calendar.MONTH)+1);//首个还款月
		map.put("fDay", calendar.get(Calendar.DAY_OF_MONTH));});//首个还款日
		
		return map;
	}

	public static void main(String[] args) {
		Date date = new Date();
		Calendar now =  Calendar.getInstance();
		now.setTime(date);

		System.out.println(now.get(Calendar.YEAR));
	}
	
}
